'use strict';

const BaseController = require('./base');

class AreaController extends BaseController {
  get _service() {
    return 'area';
  }

  async create() {
    const { ctx } = this;
    const { name, level, parentId } = ctx.params();
    const result = await this.service.area.create({ name, level, parentId });
    if (result) {
      this.success();
    } else {
      this.error('创建角色失败');
    }
  }

  async update() {
    const { ctx } = this;
    const { name } = ctx.params();
    const { id } = ctx.params;
    const result = await this.service.area.update({ name, id });
    if (result) {
      this.success();
    } else {
      this.error('更新地区失败');
    }
  }

  async destroy() {
    await this._destory('删除地区失败');
  }

  async show() {
    await this._show('获取地区失败');
  }

  async index() {
    const { ctx } = this;
    const tempResult = await this.service.area.findAll();
    const result = ctx.helper.convert(tempResult);
    if (result) {
      this.success(result);
    } else {
      this.error('获取地区失败');
    }
  }
}

module.exports = AreaController;
